﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Report_WeekReport : System.Web.UI.Page
{
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    WeekDateManager dllWeekDateManager = new WeekDateManager();
    ReportManager dllReportManager = new ReportManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpQuarterDate);
        }
        this.gdvWeekReport.DataSource = new DataTable();
        this.gdvWeekReport.DataBind();
    }

    /// <summary>
    /// 获得握手关系
    /// </summary>
    /// <returns></returns>
    DataTable getMonthHandShake()
    {
        ListItem li = this.drpWeekDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无周周期")
            {
                DataTable _weekHandShake = dllReportManager.getWeekHandShake(Convert.ToInt32(li.Value));
                return _weekHandShake;
            }
        }
        return new DataTable();
    }

    void bindGridView()
    {
        ListItem li = this.drpWeekDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无周周期")
            {
                DataSet ds = dllReportManager.getWeekReport(Convert.ToInt32(li.Value));
                DataTable _resultTable = ds.Tables[ds.Tables.Count - 1];
                DataTable _weekHandShake = getMonthHandShake();
                for (int i = 0; i < _weekHandShake.Rows.Count; i++)
                {
                    DataRow dr = _resultTable.NewRow();
                    for (int j = 0; j < _resultTable.Columns.Count; j++)
                    {
                        dr[j] = _weekHandShake.Rows[i][j].ToString();
                    }
                    _resultTable.Rows.InsertAt(dr, 0);
                }
                DataTable WeekReport = ds.Tables[ds.Tables.Count - 1];
                DataRow _dataRow = WeekReport.NewRow();
                _dataRow["DepartmentParentName"] = "每条计划占比";
                _dataRow["DepartmentName"] = "每条计划占比";
                WeekReport.Rows.Add(_dataRow);

                //添加未完成项影响总比率列
                DataColumn dc = new DataColumn("部门计划未完成率");
                WeekReport.Columns.Add(dc);
                double TheWeekPlanCount = 0;
                double TheWeekCompleteCount = 0;
                double TheWeekEmphasisPlanCount = 0;
                double PercentUnSuccess = 0;

                for (int i = 0; i < WeekReport.Rows.Count - 1; i++)
                {
                    if (WeekReport.Rows[i][2].ToString().Equals("公司完成合计") || WeekReport.Rows[i][2].ToString().Equals("公司完成百分比") || WeekReport.Rows[i][2].ToString().Equals("计划完成合计") || WeekReport.Rows[i][2].ToString().Equals("计划完成百分比"))
                    {
                    }
                    else
                    {
                        TheWeekPlanCount = Convert.ToInt32(WeekReport.Rows[i]["TheWeekPlanCount"].ToString());
                        TheWeekCompleteCount = Convert.ToInt32(WeekReport.Rows[i]["TheWeekCompleteCount"].ToString());
                        TheWeekEmphasisPlanCount = Convert.ToInt32(WeekReport.Rows[i]["TheEmphasisPlan"].ToString());
                        TheWeekPlanCount = TheWeekPlanCount - TheWeekEmphasisPlanCount;
                        if (TheWeekPlanCount > 0)
                        {
                            PercentUnSuccess = (TheWeekCompleteCount / TheWeekPlanCount) * 100;
                            if (PercentUnSuccess > 0)
                            {
                                WeekReport.Rows[i]["部门计划未完成率"] = PercentUnSuccess.ToString("0.00");
                            }
                            else
                            {
                                WeekReport.Rows[i]["部门计划未完成率"] = "";
                            }
                        }
                    }
                }

                double SumPercent = 0;
                this.gdvWeekReport.DataSource = WeekReport;
                this.gdvWeekReport.DataBind();
                MergeRow(this.gdvWeekReport, 0);
                Process(this.gdvWeekReport);
            }
        }
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.drpWeekDate.SelectedValue);
        int _preWeekDate = dllWeekDateManager.getPreWeekDate(_value);
        #region 添加链接
        for (int i = 1; i < this.gdvWeekReport.Rows.Count - 1; i++)
        {
            if (this.gdvWeekReport.Rows[i].Cells[1].Text != "公司完成百分比" && this.gdvWeekReport.Rows[i].Cells[2].Text != "计划完成百分比" && this.gdvWeekReport.Rows[i].Cells[2].Text != "&nbsp;")
            {
                if (this.gdvWeekReport.Rows[i].Cells[1].Text == "计划完成合计" || this.gdvWeekReport.Rows[i].Cells[1].Text == "公司完成合计")
                {
                }
                else
                {
                    //上周总数                    
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[2].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += _preWeekDate + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=1' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[2].Text = _url + this.gdvWeekReport.Rows[i].Cells[2].Text + "</a>";
                    }
                    //上周完成总数
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[3].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += _preWeekDate + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=4' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[3].Text = _url + this.gdvWeekReport.Rows[i].Cells[3].Text + "</a>";
                    }
                    //本周总数
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[5].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += Convert.ToInt32(this.drpWeekDate.SelectedValue) + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=1' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[5].Text = _url + this.gdvWeekReport.Rows[i].Cells[5].Text + "</a>";
                    }
                    //本周完成总数
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[6].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += Convert.ToInt32(this.drpWeekDate.SelectedValue) + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=4' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[6].Text = _url + this.gdvWeekReport.Rows[i].Cells[6].Text + "</a>";
                    }
                    //本周顺利总数
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[7].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += Convert.ToInt32(this.drpWeekDate.SelectedValue) + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=5' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[7].Text = _url + this.gdvWeekReport.Rows[i].Cells[7].Text + "</a>";
                    }
                    //本周障碍总数
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[8].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += Convert.ToInt32(this.drpWeekDate.SelectedValue) + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=6' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[8].Text = _url + this.gdvWeekReport.Rows[i].Cells[8].Text + "</a>";
                    }
                    //本周失控总数
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[9].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += Convert.ToInt32(this.drpWeekDate.SelectedValue) + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=7' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[9].Text = _url + this.gdvWeekReport.Rows[i].Cells[9].Text + "</a>";
                    }
                    //本周调整总数
                    if (Convert.ToInt32(this.gdvWeekReport.Rows[i].Cells[10].Text) > 0)
                    {
                        string _department = Server.UrlEncode(this.gdvWeekReport.Rows[i].Cells[1].Text);
                        string _url = "<a href='ViewPlan.aspx?PlanType=3&WeekDate=";
                        _url += Convert.ToInt32(this.drpWeekDate.SelectedValue) + "&_department=";
                        _url += _department;
                        _url += "&PlanSendState=8' target='_blank'>";
                        this.gdvWeekReport.Rows[i].Cells[10].Text = _url + this.gdvWeekReport.Rows[i].Cells[10].Text + "</a>";
                    }
                }
            }
        }
        #endregion

        #region 添加握手链接
        if (this.gdvWeekReport.Rows.Count >= 1)
        {
            if (this.gdvWeekReport.Rows[0].Cells[0].Text == "握手关系")
            {
                if (Convert.ToInt32(this.gdvWeekReport.Rows[0].Cells[2].Text) > 0)
                {
                    string _url = "<a href='ViewHandShake.aspx?Type=2&WeekDate=";
                    _url += _preWeekDate;
                    _url += "&PlanSendState=1' target='_blank'>";
                    this.gdvWeekReport.Rows[0].Cells[2].Text = _url + this.gdvWeekReport.Rows[0].Cells[2].Text + "</a>";
                }
                if (Convert.ToInt32(this.gdvWeekReport.Rows[0].Cells[3].Text) > 0)
                {
                    string _url = "<a href='ViewHandShake.aspx?Type=2&WeekDate=";
                    _url += _preWeekDate;
                    _url += "&PlanSendState=4' target='_blank'>";
                    this.gdvWeekReport.Rows[0].Cells[3].Text = _url + this.gdvWeekReport.Rows[0].Cells[3].Text + "</a>";
                }
                string a = this.gdvWeekReport.Rows[0].Cells[4].Text;
                if (Convert.ToInt32(this.gdvWeekReport.Rows[0].Cells[4].Text) > 0)
                {
                    string _url = "<a href='ViewHandShake.aspx?Type=2&WeekDate=";
                    _url += Convert.ToInt32(this.drpWeekDate.SelectedValue);
                    _url += "&PlanSendState=1' target='_blank'>";
                    this.gdvWeekReport.Rows[0].Cells[4].Text = _url + this.gdvWeekReport.Rows[0].Cells[4].Text + "</a>";
                }
                if (Convert.ToInt32(this.gdvWeekReport.Rows[0].Cells[5].Text) > 0)
                {
                    string _url = "<a href='ViewHandShake.aspx?Type=2&WeekDate=";
                    _url += Convert.ToInt32(this.drpWeekDate.SelectedValue);
                    _url += "&PlanSendState=4' target='_blank'>";
                    this.gdvWeekReport.Rows[0].Cells[5].Text = _url + this.gdvWeekReport.Rows[0].Cells[5].Text + "</a>";
                }
                if (Convert.ToInt32(this.gdvWeekReport.Rows[0].Cells[6].Text) > 0)
                {
                    string _url = "<a href='ViewHandShake.aspx?Type=2&WeekDate=";
                    _url += Convert.ToInt32(this.drpWeekDate.SelectedValue);
                    _url += "&PlanSendState=5' target='_blank'>";
                    this.gdvWeekReport.Rows[0].Cells[6].Text = _url + this.gdvWeekReport.Rows[0].Cells[6].Text + "</a>";
                }
                if (Convert.ToInt32(this.gdvWeekReport.Rows[0].Cells[7].Text) > 0)
                {
                    string _url = "<a href='ViewHandShake.aspx?Type=2&WeekDate=";
                    _url += Convert.ToInt32(this.drpWeekDate.SelectedValue);
                    _url += "&PlanSendState=6' target='_blank'>";
                    this.gdvWeekReport.Rows[0].Cells[7].Text = _url + this.gdvWeekReport.Rows[0].Cells[7].Text + "</a>";
                }
                if (Convert.ToInt32(this.gdvWeekReport.Rows[0].Cells[8].Text) > 0)
                {
                    string _url = "<a href='ViewHandShake.aspx?Type=2&WeekDate=";
                    _url += Convert.ToInt32(this.drpWeekDate.SelectedValue);
                    _url += "&PlanSendState=7' target='_blank'>";
                    this.gdvWeekReport.Rows[0].Cells[8].Text = _url + this.gdvWeekReport.Rows[0].Cells[8].Text + "</a>";
                }
            }
        }
        #endregion
    }


    protected void btnGetReport_Click(object sender, EventArgs e)
    {
        bindGridView();
        if (this.gdvWeekReport.Rows.Count > 0)
        {

            this.gdvWeekReport.Rows[0].Cells[0].ColumnSpan = 2;
            this.gdvWeekReport.Rows[0].Cells[1].Visible = false;
            this.gdvWeekReport.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 2].Cells[0].ColumnSpan = 2;
            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 2].Cells[1].Visible = false;
            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 2].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 1].Cells[0].ColumnSpan = 2;
            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 1].Cells[1].Visible = false;
            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 1].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 3].Cells[0].ColumnSpan = 2;
            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 3].Cells[1].Visible = false;
            this.gdvWeekReport.Rows[this.gdvWeekReport.Rows.Count - 3].Cells[0].HorizontalAlign = HorizontalAlign.Center;

            this.gdvWeekReport.Columns[0].HeaderStyle.Width = 90;
            this.gdvWeekReport.Columns[0].ItemStyle.Width = 90;
            this.gdvWeekReport.Columns[1].HeaderStyle.Width = 90;
            this.gdvWeekReport.Columns[1].ItemStyle.Width = 90;
        }
    }

    void Process(GridView _gridView)
    {
        _gridView.Columns[3].HeaderStyle.BackColor = System.Drawing.Color.FromName("#00C0C0"); 
        _gridView.Columns[6].HeaderStyle.BackColor = System.Drawing.Color.FromName("#00C0C0");
        _gridView.Columns[7].HeaderStyle.BackColor = System.Drawing.Color.FromName("#00C000");
        _gridView.Columns[8].HeaderStyle.BackColor = System.Drawing.Color.Yellow;
        _gridView.Columns[9].HeaderStyle.BackColor = System.Drawing.Color.Red;
        _gridView.Columns[3].ItemStyle.BackColor = System.Drawing.Color.FromName("#00C0C0"); 
        _gridView.Columns[6].ItemStyle.BackColor = System.Drawing.Color.FromName("#00C0C0");
        _gridView.Columns[7].ItemStyle.BackColor = System.Drawing.Color.FromName("#00C000");
        _gridView.Columns[8].ItemStyle.BackColor = System.Drawing.Color.Yellow;
        _gridView.Columns[9].ItemStyle.BackColor = System.Drawing.Color.Red;
        _gridView.Columns[4].ItemStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        _gridView.Columns[4].HeaderStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        _gridView.Columns[10].HeaderStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        _gridView.Columns[10].ItemStyle.BackColor = System.Drawing.Color.FromName("#c0c0c0");
        System.Drawing.Color[] _color = new System.Drawing.Color[5];
        _color[0] = System.Drawing.Color.Gray;
        _color[1] = System.Drawing.Color.Cornsilk;
        _color[2] = System.Drawing.Color.Brown;
        _color[3] = System.Drawing.Color.Orange;
        _color[4] = System.Drawing.Color.Purple;
        _gridView.Rows[1].Cells[0].BackColor = _color[0];
        for (int i = 0; i < _gridView.Rows.Count; i++)
        {
            if (_gridView.Rows[i].Cells[1].Text == "计划完成百分比" || _gridView.Rows[i].Cells[1].Text == "公司完成百分比")
            {
                double percent = Convert.ToDouble(_gridView.Rows[i].Cells[3].Text);

                _gridView.Rows[i].Cells[3].Text = string.Format("{0:P}", percent);
                percent = Convert.ToDouble(_gridView.Rows[i].Cells[6].Text);

                _gridView.Rows[i].Cells[6].Text = string.Format("{0:P}", percent);
            }
        }
        int _index = 0;
        for (int i = 1; i < _gridView.Rows.Count; i++)
        {

            if (_gridView.Rows[i].Cells[1].Text == "计划完成合计")
            {
                _gridView.Rows[i].BackColor = _color[_index];
                _gridView.Rows[i].Cells[3].BackColor = _color[_index];
                _gridView.Rows[i].Cells[4].BackColor = _color[_index];
                _gridView.Rows[i].Cells[5].BackColor = _color[_index];
                _gridView.Rows[i].Cells[6].BackColor = _color[_index];
                _gridView.Rows[i].Cells[7].BackColor = _color[_index];
                _gridView.Rows[i].Cells[8].BackColor = _color[_index];
                _gridView.Rows[i].Cells[9].BackColor = _color[_index];
                _gridView.Rows[i].Cells[10].BackColor = _color[_index];
            }
            if (_gridView.Rows[i].Cells[1].Text == "计划完成百分比")
            {
                _gridView.Rows[i].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[3].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[4].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[5].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[6].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[7].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[8].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[9].BackColor = _gridView.Rows[i - 1].BackColor;
                _gridView.Rows[i].Cells[10].BackColor = _gridView.Rows[i - 1].BackColor;
                _index++;
                if (_index > _color.Length - 1)
                {
                    _index = 0;
                }
                _gridView.Rows[i + 1].Cells[0].BackColor = _color[_index];
            }
        }
        _gridView.Rows[_gridView.Rows.Count - 3].Cells[0].BackColor = new System.Drawing.Color();
        _gridView.Rows[_gridView.Rows.Count - 3].Cells[3].BackColor = System.Drawing.Color.FromName("#ffffff");

        _gridView.Rows[_gridView.Rows.Count - 1].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[3].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[5].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[6].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[7].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[8].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[10].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[4].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[9].BackColor = System.Drawing.Color.Red;


        _gridView.Rows[_gridView.Rows.Count - 2].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[3].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[5].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[6].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[7].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[8].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[10].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[4].BackColor = System.Drawing.Color.Red;
        _gridView.Rows[_gridView.Rows.Count - 2].Cells[9].BackColor = System.Drawing.Color.Red;

        #region 处理计划占比
        double _preCount = Convert.ToInt32(_gridView.Rows[_gridView.Rows.Count - 3].Cells[3].Text);
        double _Count = Convert.ToInt32(_gridView.Rows[_gridView.Rows.Count - 3].Cells[5].Text);
        double _prePlan = 0.0;
        double _Plan = 0.0;
        if (_preCount != 0)
        {
            _prePlan = 1 / _preCount;
        }

        _prePlan = _prePlan * 100;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[2].Text = _prePlan.ToString("0.00") + "%";
        if (_Count != 0)
        {
            _Plan = 1 / _Count;
        }
        _Plan = _Plan * 100;
        _gridView.Rows[_gridView.Rows.Count - 1].Cells[5].Text = _Plan.ToString("0.00") + "%";
        #endregion
    }

    /// <summary>
    /// 合并同一列相同值得行
    /// </summary>
    /// <param name="_gridView"></param>
    /// <param name="_cellNum"></param>
    void MergeRow(GridView _gridView, int _cellNum)
    {
        int i = 0, rowSpanNum = 1;
        //遍历GridView
        while (i < _gridView.Rows.Count - 1)
        {
            //获得当前行
            GridViewRow gvr = _gridView.Rows[i];
            for (++i; i < _gridView.Rows.Count; i++)
            {
                //获得下一行
                GridViewRow gvrNext = _gridView.Rows[i];
                if (gvr.Cells[_cellNum].Text == gvrNext.Cells[_cellNum].Text)
                {
                    gvrNext.Cells[_cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[_cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                }
                //遍历完毕进行合并
                if (i == _gridView.Rows.Count - 1)
                {
                    gvr.Cells[_cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }




    /// <summary>
    /// 合并列头
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvMonthReport_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            rowHeader.BackColor = System.Drawing.Color.White;
            rowHeader.Font.Bold = true;
            TableCellCollection cells = e.Row.Cells;
            int i = 0;
            int colSpanNum = 1;
            TableCell headerCell = cells[i];
            for (++i; i < cells.Count; i++)
            {
                TableCell headerNextCell = cells[i];
                if (headerCell.Text == headerNextCell.Text)
                {
                    headerNextCell.Visible = false;
                    colSpanNum++;
                }
                else
                {
                    headerCell.ColumnSpan = colSpanNum;
                    colSpanNum = 1;
                    break;
                }
                //遍历完毕进行合并
                if (i == cells.Count - 1)
                {
                    headerCell.ColumnSpan = colSpanNum;
                }
            }


            this.gdvWeekReport.Controls[0].Controls.AddAt(0, rowHeader);
            TableCell headerCellMonth = new TableCell();
            headerCellMonth.Text = "";
            headerCellMonth.ColumnSpan = 2;
            rowHeader.Cells.Add(headerCellMonth);

            headerCellMonth = new TableCell();
            headerCellMonth.Text = "上周";
            headerCellMonth.ColumnSpan = 3;
            headerCellMonth.HorizontalAlign = HorizontalAlign.Center;
            rowHeader.Cells.Add(headerCellMonth);

            headerCellMonth = new TableCell();
            headerCellMonth.Text = "本周";
            headerCellMonth.ColumnSpan = cells.Count - 1;
            headerCellMonth.HorizontalAlign = HorizontalAlign.Center;

            rowHeader.Cells.Add(headerCellMonth);
            rowHeader.Visible = true;
            this.gdvWeekReport.Controls[0].Controls.AddAt(0, rowHeader);
        }
    }
    /// <summary>
    /// 绑定dropdownlist
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        //年份绑定
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }
            //根据系统日期自动绑定到当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));

        }

        //绑定年计划周期
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                }
                #endregion
                bindDropDownList(this.drpMonthDate);
            }
            else
            {
                ListItem _li = new ListItem("无年周期");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(_li);
                bindDropDownList(this.drpMonthDate);
            }
        }
        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {
            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                this.drpMonthDate.DataSource = _month;
                this.drpMonthDate.DataTextField = "MonthName";
                this.drpMonthDate.DataValueField = "Month_Id";
                this.drpMonthDate.DataBind();
                #region 根据系统当前日期绑定到当前月周期
                DataTable _monthtime = dllMonthDateManager.getMonthDate();
                if (_monthtime.Rows.Count == 1)
                {
                    this.drpMonthDate.SelectedIndex = this.drpMonthDate.Items.IndexOf(this.drpMonthDate.Items.FindByText(_monthtime.Rows[0][3].ToString()));
                }
                #endregion
                bindDropDownList(this.drpWeekDate);
            }
            else
            {
                ListItem _li = new ListItem("无月周期");
                this.drpMonthDate.Items.Clear();
                this.drpMonthDate.Items.Add(_li);
                bindDropDownList(this.drpWeekDate);
            }

        }
        //绑定周计划周期
        if (_drp.Equals(this.drpWeekDate))
        {
            object[] _value = new object[3];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            _value[2] = this.drpMonthDate.SelectedItem.Text;
            DataTable _week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
            if (_week.Rows.Count != 0)
            {
                this.drpWeekDate.DataSource = _week;
                this.drpWeekDate.DataTextField = "WeekName";
                this.drpWeekDate.DataValueField = "Week_Id";
                this.drpWeekDate.DataBind();
                DataTable _weektime = dllWeekDateManager.getWeekDate();
                if (_weektime.Rows.Count == 1)
                {
                    this.drpWeekDate.SelectedIndex = this.drpWeekDate.Items.IndexOf(this.drpWeekDate.Items.FindByText(_weektime.Rows[0][4].ToString()));
                    if (Request.Params["Id"] != null && Request.Params["Id"].ToString().Length != 0)
                    {

                        this.drpWeekDate.SelectedIndex = this.drpWeekDate.Items.IndexOf(this.drpWeekDate.Items.FindByValue(Request.Params["Id"].ToString()));

                    }

                }

            }
            else
            {
                ListItem _li = new ListItem("无周周期");
                this.drpWeekDate.Items.Clear();
                this.drpWeekDate.Items.Add(_li);

            }
        }
    }
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        bindDropDownList(this.drpMonthDate);
        bindDropDownList(this.drpWeekDate);
        this.gdvWeekReport.DataSource = new DataTable();
        this.gdvWeekReport.DataBind();
    }
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);
        bindDropDownList(this.drpWeekDate);
        this.gdvWeekReport.DataSource = new DataTable();
        this.gdvWeekReport.DataBind();
    }
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpWeekDate);
        this.gdvWeekReport.DataSource = new DataTable();
        this.gdvWeekReport.DataBind();
    }


    /// <summary>
    /// 周完成率
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnWeekMonth_Click(object sender, EventArgs e)
    {
        ListItem li = this.drpWeekDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无周周期")
            {
                string _url = "WeekReportCompleteRate.aspx?WeekDate=" + Convert.ToInt32(li.Value);
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=740,width=1015,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            }
        }
    }


    /// <summary>
    /// 部门完成率
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnWeekDepartment_Click(object sender, EventArgs e)
    {
        ListItem li = this.drpWeekDate.SelectedItem;
        if (li != null)
        {
            if (li.Text != "无周周期")
            {
                string _url = "WeekDepartmentCompleteRate.aspx?WeekDate=" + Convert.ToInt32(li.Value);
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=740,width=1015,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            }
        }
    }


    /// <summary>
    /// 周计划统计投影事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        string _url = "WeekReportDisplay.aspx";
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=715,width=1015,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        GridView[] _gridView = new GridView[1];
        bindGridView();
        MergeRow(this.gdvWeekReport, 0);
        _gridView[0] = this.gdvWeekReport;
        Utility.Comm.WeekReportToExcel(GridViewToDataTable(_gridView)[0], this.drpQuarterDate.SelectedItem.Text + this.drpMonthDate.SelectedItem.Text + this.drpWeekDate.SelectedItem.Text);
    }

    /// <summary>
    /// GridView转DataTable
    /// </summary>
    DataTable[] GridViewToDataTable(GridView[] _sourceGridView)
    {
        DataTable[] _targetDataTable = new DataTable[_sourceGridView.Length];
        for (int i = 0; i < _targetDataTable.Length; i++)
        {
            _targetDataTable[i] = new DataTable();
            DataColumn dc = new DataColumn();
            GridViewRow gr = _sourceGridView[i].HeaderRow;
            //根据源GridView循环创建目标DataTable列
            for (int j = 0; j < gr.Cells.Count; j++)
            {
                //if (gr.Cells[j].Visible == true)
                //{
                dc = new DataColumn(gr.Cells[j].Text);
                if (dc.ColumnName == "计划总数" || dc.ColumnName == "圆满完成" || dc.ColumnName == "部门")
                {
                    Guid gd = Guid.NewGuid();
                    dc.ColumnName = dc.ColumnName + gd.ToString();
                }
                _targetDataTable[i].Columns.Add(dc);
                //}
            }
            //根据源GridView循环创建目标DataTable行
            for (int j = 0; j < _sourceGridView[i].Rows.Count; j++)
            {
                DataRow dr = _targetDataTable[i].NewRow();
                for (int k = 0; k < gr.Cells.Count; k++)
                {

                    if (k < _targetDataTable[i].Columns.Count)
                    {

                        dr[k] = _sourceGridView[i].Rows[j].Cells[k].Text;
                    }
                }
                _targetDataTable[i].Rows.Add(dr);
            }
        }
        return _targetDataTable;
    }
}
